package com.cinema;

import com.cinema.service.CinemaServiceInterface;
import com.cinema.service.CinemaServiceDB;
import com.cinema.entity.*;
import com.cinema.exception.SeatException;

import java.time.LocalDateTime;

/**
 * 基于数据库的电影院系统测试用例
 */
public class CinemaSystemDB {
    
    public static void main(String[] args) {
        System.out.println("=== 第四轮迭代：数据持久化测试 ===");
        
        CinemaServiceInterface service = new CinemaServiceDB();
        
        try {
            // 测试用户管理
            testUserManagement(service);
            
            // 测试电影管理
            testMovieManagement(service);
            
            // 测试放映厅管理
            testRoomManagement(service);
            
            // 测试排片管理
            testScheduleManagement(service);
            
            // 测试售票功能
            testTicketSales(service);
            
            // 测试查询功能
            testQueryFunctions(service);
            
            System.out.println("\n=== 所有测试通过！数据持久化功能正常 ===");
            
        } catch (Exception e) {
            System.err.println("测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static void testUserManagement(CinemaServiceInterface service) {
        System.out.println("\n--- 测试用户管理 ---");
        
        // 添加用户
        User admin = new User("admin", "admin", Role.ADMIN, null);
        User manager = new User("manager", "manager", Role.MANAGER, "admin");
        User cashier = new User("cashier", "cashier", Role.CASHIER, "manager");
        
        service.addUser(admin);
        service.addUser(manager);
        service.addUser(cashier);
        
        System.out.println("✓ 添加用户成功");
        
        // 查询用户
        User foundAdmin = service.getUser("admin");
        System.out.println("✓ 查询用户: " + foundAdmin);
        
        // 查询所有用户
        var allUsers = service.getAllUsers();
        System.out.println("✓ 所有用户数量: " + allUsers.size());
        
        // 更新用户
        manager.setUsername("new_manager");
        service.updateUser(manager);
        System.out.println("✓ 更新用户成功");
        
        // 删除用户
        service.deleteUser("cashier");
        System.out.println("✓ 删除用户成功");
    }
    
    private static void testMovieManagement(CinemaServiceInterface service) {
        System.out.println("\n--- 测试电影管理 ---");
        
        // 添加电影
        Movie movie1 = new Movie("m1", "流浪地球", 120);
        Movie movie2 = new Movie("m2", "战狼", 90);
        
        service.addMovie(movie1, service.getUser("admin"));
        service.addMovie(movie2, service.getUser("admin"));
        
        System.out.println("✓ 添加电影成功");
        
        // 查询电影
        Movie foundMovie = service.getMovie("m1");
        System.out.println("✓ 查询电影: " + foundMovie);
        
        // 查询所有电影
        var allMovies = service.getAllMovies();
        System.out.println("✓ 所有电影数量: " + allMovies.size());
    }
    
    private static void testRoomManagement(CinemaServiceInterface service) {
        System.out.println("\n--- 测试放映厅管理 ---");
        
        // 添加放映厅
        ScreeningRoom room1 = new ScreeningRoom("r1", "IMAX厅", 10, 15);
        ScreeningRoom room2 = new ScreeningRoom("r2", "普通厅", 8, 12);
        
        service.addRoom(room1, service.getUser("admin"));
        service.addRoom(room2, service.getUser("admin"));
        
        System.out.println("✓ 添加放映厅成功");
        
        // 查询放映厅
        ScreeningRoom foundRoom = service.getRoom("r1");
        System.out.println("✓ 查询放映厅: " + foundRoom);
        
        // 查询所有放映厅
        var allRooms = service.getAllRooms();
        System.out.println("✓ 所有放映厅数量: " + allRooms.size());
        
        // 删除放映厅
        service.deleteRoom("r2", service.getUser("admin"));
        System.out.println("✓ 删除放映厅成功");
    }
    
    private static void testScheduleManagement(CinemaServiceInterface service) {
        System.out.println("\n--- 测试排片管理 ---");
        
        // 获取电影和放映厅
        Movie movie = service.getMovie("m1");
        ScreeningRoom room = service.getRoom("r1");
        
        // 添加排片
        LocalDateTime startTime = LocalDateTime.now().plusDays(1).withHour(14).withMinute(0);
        LocalDateTime endTime = startTime.plusMinutes(movie.getDuration());
        
        Schedule schedule1 = new Schedule("s1", movie, room, startTime, endTime);
        service.addSchedule(schedule1, service.getUser("manager"));
        
        System.out.println("✓ 添加排片成功");
        
        // 查询排片
        Schedule foundSchedule = service.getSchedule("s1");
        System.out.println("✓ 查询排片: " + foundSchedule);
        
        // 查询所有排片
        var allSchedules = service.getAllSchedules();
        System.out.println("✓ 所有排片数量: " + allSchedules.size());
        
        // 删除排片
        service.deleteSchedule("s1", service.getUser("manager"));
        System.out.println("✓ 删除排片成功");
    }
    
    private static void testTicketSales(CinemaServiceInterface service) {
        System.out.println("\n--- 测试售票功能 ---");
        
        // 重新添加排片用于售票测试
        Movie movie = service.getMovie("m1");
        ScreeningRoom room = service.getRoom("r1");
        LocalDateTime startTime = LocalDateTime.now().plusDays(1).withHour(14).withMinute(0);
        LocalDateTime endTime = startTime.plusMinutes(movie.getDuration());
        Schedule schedule = new Schedule("s2", movie, room, startTime, endTime);
        service.addSchedule(schedule, service.getUser("manager"));
        
        // 售票
        try {
            service.sellTicket("s2", 3, 5, "13800138000", service.getUser("cashier"));
            System.out.println("✓ 售票成功");
            
            // 查询座位状态
            boolean[][] seatStatus = service.getSeatStatus("s2");
            System.out.println("✓ 座位状态查询成功，行数: " + seatStatus.length + ", 列数: " + seatStatus[0].length);
            
            // 查询座位统计
            var stats = service.getSeatStatistics("s2");
            System.out.println("✓ 座位统计: 总数=" + stats.getTotalSeats() + ", 已售=" + stats.getOccupiedSeats());
            
            // 查询售票记录
            var records = service.getTicketRecords("s2");
            System.out.println("✓ 售票记录数量: " + records.size());
            
        } catch (SeatException e) {
            System.err.println("售票失败: " + e.getMessage());
        }
    }
    
    private static void testQueryFunctions(CinemaServiceInterface service) {
        System.out.println("\n--- 测试查询功能 ---");
        
        // 按电影查询排片
        var schedulesByMovie = service.queryByMovie("流浪地球");
        System.out.println("✓ 按电影查询排片: " + schedulesByMovie.size() + " 个");
        
        // 按放映厅查询排片
        var schedulesByRoom = service.queryByRoom("r1");
        System.out.println("✓ 按放映厅查询排片: " + schedulesByRoom.size() + " 个");
        
        // 按时间查询排片
        LocalDateTime start = LocalDateTime.now();
        LocalDateTime end = LocalDateTime.now().plusDays(7);
        var schedulesByTime = service.queryByTime(start, end);
        System.out.println("✓ 按时间查询排片: " + schedulesByTime.size() + " 个");
        
        // 查询当前上映电影
        var currentMovies = service.getCurrentMovies();
        System.out.println("✓ 当前上映电影: " + currentMovies.size() + " 部");
    }
} 